package com.woniuxy.boot.nb_feedback.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.woniuxy.boot.common.dto.FeedbackDTO;
import com.woniuxy.boot.common.vo.DisposeVO;
import com.woniuxy.boot.nb_feedback.bean.Feedback;
import com.woniuxy.boot.nb_feedback.feign.DistributedIdFeign;
import com.woniuxy.boot.nb_feedback.mapper.FeedbackMapper;
import com.woniuxy.boot.nb_feedback.service.FeedbackService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;

@Service
public class FeedbackServiceImpl extends ServiceImpl<FeedbackMapper, Feedback> implements FeedbackService {
    @Autowired
    private FeedbackMapper feedbackMapper;

    @Autowired
    private DistributedIdFeign distributedIdFeign;

    @Override
    public void addFeedback(Feedback feedback) {
        feedback.setId(distributedIdFeign.get());
        feedback.setCommitTime(LocalDateTime.now());
        feedback.setIsDispose("a");
        feedbackMapper.insert(feedback);
    }

    @Override
    public List<Feedback> getFeedbackById(Long userId) {
        LambdaQueryWrapper<Feedback> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(Feedback::getUserId, userId);
        return feedbackMapper.selectList(lambdaQueryWrapper);
    }

    @Override
    public void disposeFeedback(DisposeVO disposeVO) {
        LambdaQueryWrapper<Feedback> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(Feedback::getId, disposeVO.getId());
        Feedback feedback = feedbackMapper.selectOne(lambdaQueryWrapper);
        feedback.setIsDispose("b");
        feedback.setScheme(disposeVO.getScheme());
        feedback.setAdminId(disposeVO.getAdminId());
        feedbackMapper.insertOrUpdate(feedback);
    }

    @Override
    public List<FeedbackDTO> getAllFeedback() {
        List<Feedback> feedbackList = feedbackMapper.selectList(null);
        List<FeedbackDTO> feedbackDTOList = new ArrayList<>();
        for (Feedback feedback : feedbackList) {
            FeedbackDTO feedbackDTO=new FeedbackDTO();
            BeanUtils.copyProperties(feedback,feedbackDTO);
            feedbackDTOList.add(feedbackDTO);
        }
        return feedbackDTOList;
    }


}
